package cn.yzgy.yzgyoa.common.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.dto.SysDictKeyDto;
import cn.yzgy.yzgyoa.common.entity.SysDictKey;
import cn.yzgy.yzgyoa.common.service.ISysDictKeyService;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2021-07-14
 */
@Slf4j
@Syslog
@RestController
@RequestMapping("/common/sys-dict-key")
public class SysDictKeyController extends BaseController {

    private ISysDictKeyService sysDictKeyService;

    public SysDictKeyController(ISysDictKeyService sysDictKeyService) {
        this.sysDictKeyService = sysDictKeyService;
    }

    @ApiDesc("数据字典列表")
    @GetMapping("getAllDictKey")
    @RequiresPermissions("api:dict:dictKey:getAllDictKey")
    public Urmf getAllDictKey(@RequestParam Integer page, @RequestParam Integer size, SysDictKeyDto queryDto) {
        IPage<SysDictKeyDto> dictPage = sysDictKeyService.getAllDictKey(new Page<>(page, size), queryDto);
        return Urmf.success(getCurrentMethodName(), dictPage);
    }

    @ApiDesc("创建数据字典类型")
    @PostMapping("createDictKey")
    @RequiresPermissions("api:dict:dictKey:createDictKey")
    public Urmf createDictKey(@RequestBody SysDictKey queryDto) {
        UserInfoDetail userInfoDetail = getCurrentUser();
        boolean state = sysDictKeyService.createDictKey(userInfoDetail, queryDto);
        if(state){
            return Urmf.success(getCurrentMethodName(), "创建数据字典成功！");
        }else{
            return Urmf.fail(getCurrentMethodName(), "创建数据字典失败！");
        }

    }
    @ApiDesc("修改数据字典类型")
    @PutMapping("updateDictKey")
    @RequiresPermissions("api:dict:dictKey:updateDictKey")
    public Urmf updateDictKey(@RequestBody SysDictKey queryDto) {
        UserInfoDetail userInfoDetail = getCurrentUser();
        boolean state = sysDictKeyService.updateDictKey(userInfoDetail, queryDto);
        if(state){
            return Urmf.success(getCurrentMethodName(), "修改数据字典成功！");
        }else{
            return Urmf.fail(getCurrentMethodName(), "修改数据字典失败！");
        }
    }

    @ApiDesc("级联删除数据字典")
    @DeleteMapping("delDictKey")
    @RequiresPermissions("api:dict:dictKey:delDictKey")
    public Urmf delDictKey(@RequestBody SysDictKeyDto delDto) {
        boolean state = sysDictKeyService.delDictKey(delDto);
        if(state){
            return Urmf.success(getCurrentMethodName(), "级联删除数据字典成功！");
        }else{
            return Urmf.fail(getCurrentMethodName(), "级联删除数据字典失败！");
        }

    }
}
